<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_news.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["nw_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./modules/news/functions_news.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_users.php");
$template->loadLanguageSection("news");

$interface->assignQuickLink($_SWIFT["language"]["news"], "index.php?_m=news&_a=managenews", "icon_newsitem.gif");
$interface->assignQuickLink($_SWIFT["language"]["subscribers"], "index.php?_m=news&_a=managesubscribers", "icon_subscribernotvalidated.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertnews"], "index.php?_m=news&_a=insertnews", "icon_insertnewsitem.gif");
$interface->assignQuickLink($_SWIFT["language"]["importexport"], "index.php?_m=news&_a=importexport", "icon_export.gif");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing news
*/
function _newsFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	if ($record["newstype"] == SWIFTPUBLIC)
	{
		$record["news.newstype"] = $_SWIFT["language"]["public"];
		$typeicon = "icon_newsitem.gif";
	} else {
		$record["news.newstype"] = $_SWIFT["language"]["private"];
		$typeicon = "icon_newsitemprivate.gif";
	}

	$record["news.subject"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="1"><tr><td width="1"><img src="'. $_SWIFT["themepath"] .$typeicon.'" border="0" /></td>' .'<td><span class="smalltext">&nbsp;<a href="index.php?_m=news&_a=editnews&newsid='. $record["newsid"] .'">'. $record["subject"] .'</a></span></td></tr></table>';

	$record["news.dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);
	if ($record["expiry"] != 0)
	{
		$record["news.expiry"] = edate($_SWIFT["settings"]["dt_dateformat"], $record["expiry"]);
	}


	$record["options"] = '<a href="index.php?_m=news&_a=editnews&newsid='. $record["newsid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=news&_a=managenews&do=delete&newsid='. $record["newsid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}

/**
* Mass Action: Delete News
*/
function _maDeleteNews()
{
	global $_SWIFT, $departments;

	if (!is_array($_POST["itemid"]))
	{
		return false;
	}

	deleteNews($_POST["itemid"]);
}




/**
* ###############################################
* MANAGE NEWS
* ###############################################
*/
if ($eventaction == "managenews")
{
	if ($_GET["do"] == "delete" && $_GET["newsid"] != "")
	{
		$infomessage = $_SWIFT["language"]["newsdelconfirm"];

		deleteNews(array($_GET["newsid"]));
	}

	if (trim($_REQUEST["insertnews"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["newsinsertconfirm"], htmlspecialchars($_GET["insertnews"]));
	} else if (trim($_REQUEST["updatenews"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["newsupdateconfirm"], htmlspecialchars($_GET["updatenews"]));
	}

	$options["recordsperpage"] = "8";
	$options["sortby"] = "news.dateline";
	$options["sortorder"] = "desc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteNews";

	$options["idname"] = "newsid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["newssubject"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'news` AS news WHERE news.subject LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["newsdescription"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'news` AS news WHERE news.description LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["advancedsearch"][2]["title"] = $_SWIFT["language"]["newscontents"];
	$options["advancedsearch"][2]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'news` AS news LEFT JOIN `'. TABLE_PREFIX .'newsdata` AS newsdata ON (news.newsid = newsdata.newsid) WHERE newsdata.contents LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_newsFields";

	$fields[0]["name"] = "news.subject";
	$fields[0]["title"] = $_SWIFT["language"]["newssubject"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "news.newstype";
	$fields[1]["title"] = $_SWIFT["language"]["newstype"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "news.dateline";
	$fields[2]["title"] = $_SWIFT["language"]["date"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$fields[3]["name"] = "news.expiry";
	$fields[3]["title"] = $_SWIFT["language"]["newsexpiry"];
	$fields[3]["width"] = "120";
	$fields[3]["align"] = "center";

/*	$fields[4]["type"] = "custom";
	$fields[4]["name"] = "options";
	$fields[4]["title"] = $_SWIFT["language"]["options"];
	$fields[4]["width"] = "160";
	$fields[4]["align"] = "center";*/

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["news"]." &gt; ".$_SWIFT["language"]["manage"], 7);

	$interface->staffNavBar('<a href="index.php?_m=news&_a=managenews" title="'.$_SWIFT["language"]["news"].'">'.$_SWIFT["language"]["news"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$grid->start("news", $_SWIFT["language"]["news"], 'SELECT * FROM `'. TABLE_PREFIX .'news` AS news $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'news`;', 'SELECT * FROM `'. TABLE_PREFIX .'news` AS news WHERE news.subject LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	
	$grid->display("news");

	echo '<BR /><span class="smalltext">'. $_SWIFT["language"]["legend"] .'<img src="'. $_SWIFT["themepath"] .'icon_newsitem.gif" border="0" /> '. $_SWIFT["language"]["publicnews"] .'&nbsp;&nbsp;&nbsp; <img src="'. $_SWIFT["themepath"] .'icon_newsitemprivate.gif" border="0" /> '. $_SWIFT["language"]["privatenews"].'</span>';

	$interface->staffFooter();





/**
* ###############################################
* INSERT NEWS
* ###############################################
*/
} else if ($eventaction == "insertnews") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "" || trim($_POST["description"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			if (empty($_POST["expiry"]))
			{
				$expirydateline = 0;
			} else {
				$expirydateline = getCalendarDateline($_POST["expiry"]);
			}

			insertNews($_POST["subject"], iif(empty($_POST["emailsubject"]), $_POST["subject"], $_POST["emailsubject"]), $_POST["description"], $_POST["tgroupid"], $expirydateline, iif($_POST["newstype"]==1, SWIFTPUBLIC, SWIFTPRIVATE), $_POST["contents"], iif($_POST["sendemail"]==1, true, false), $_POST["fromname"], $_POST["fromemail"]);

			printRedirect(sprintf($_SWIFT["language"]["newsinsertconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=news&_a=managenews&insertnews=".urlencode($_POST["subject"]));
			exit;
		}
	}

	if ($_SWIFT["settings"]["nw_wysiwyg"] == 1)
	{
		$template->assign("htmlarea", true);
	}

	$template->assign("calendar", true);
	$template->assign("htmlareanotable", true);

	$interface->staffHeader($_SWIFT["language"]["news"]." &gt; ".$_SWIFT["language"]["insert"], 7);

	$interface->staffNavBar('<a href="index.php?_m=news&_a=managenews" title="'.$_SWIFT["language"]["news"].'">'.$_SWIFT["language"]["news"].'</a> &raquo; <a href="index.php?_m=news&_a=insertnews" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderNewsForm(INSERT);

	$template->assign("backurl", "index.php?_m=news&_a=managenews");

	$interface->staffFooter();




/**
* ###############################################
* EDIT NEWS
* ###############################################
*/
} else if ($eventaction == "editnews") {
	$_news = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."news` AS news LEFT JOIN `". TABLE_PREFIX ."newsdata` AS newsdata ON (news.newsid = newsdata.newsid) WHERE news.newsid = '". intval($_REQUEST["newsid"]) ."';");
	if (empty($_news["newsid"]))
	{
		trigger_error($_SWIFT["language"]["invalidnewsitem"], E_USER_ERROR);
	}

	if ($_GET["do"] == "exportpdf")
	{
		makeNewsPDF($_news["newsid"], true);
		exit;
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "" || trim($_POST["description"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			if (empty($_POST["expiry"]))
			{
				$expirydateline = 0;
			} else {
				$expirydateline = getCalendarDateline($_POST["expiry"]);
			}

			updateNews($_news["newsid"], $_POST["subject"], iif(empty($_POST["emailsubject"]), $_POST["subject"], $_POST["emailsubject"]), $_POST["description"], $_POST["tgroupid"], $expirydateline, iif($_POST["newstype"]==1, SWIFTPUBLIC, SWIFTPRIVATE), $_POST["contents"], iif($_POST["sendemail"]==1, true, false), $_POST["fromname"], $_POST["fromemail"]);

			printRedirect(sprintf($_SWIFT["language"]["newsupdateconfirm"], htmlspecialchars($_POST["subject"])), "index.php?_m=news&_a=managenews&updatenews=".urlencode($_POST["subject"]));
			exit;
		}
	}

	$template->assign("itemoptiontitle", $_SWIFT["language"]["newsoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["exportpdf"], "index.php?_m=news&_a=editnews&do=exportpdf&newsid=". $_news["newsid"], "mimeico_pdf.gif");
	$interface->assignItemOption($_SWIFT["language"]["deletenews"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=news&_a=managenews&do=delete&newsid='. $_news["newsid"] .'\')');

	$_POST = array_merge($_POST, $_news);

	if ($_SWIFT["settings"]["nw_wysiwyg"] == 1)
	{
		$template->assign("htmlarea", true);
	}

	$template->assign("calendar", true);
	$template->assign("htmlareanotable", true);

	$interface->staffHeader($_SWIFT["language"]["news"]." &gt; ".$_SWIFT["language"]["edit"], 7);

	$interface->staffNavBar('<a href="index.php?_m=news&_a=managenews" title="'.$_SWIFT["language"]["news"].'">'.$_SWIFT["language"]["news"].'</a> &raquo; <a href="index.php?_m=news&_a=editnews&newsid='. intval($_news["newsid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderNewsForm(EDIT);

	$template->assign("backurl", "index.php?_m=news&_a=managenews");

	$interface->staffFooter();



/**
* ###############################################
* VIEW NEWS
* ###############################################
*/
} else if ($eventaction == "viewnews") {
	$_newsitem = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."news` AS news LEFT JOIN `". TABLE_PREFIX ."newsdata` AS newsdata ON (news.newsid = newsdata.newsid) WHERE news.newsid = '". intval($_REQUEST["newsid"]) ."';");
	if (empty($_newsitem["newsid"]))
	{
		trigger_error($_SWIFT["language"]["invalidnewsitem"], E_USER_ERROR);
	}

	$template->assign("documenttitle", $_newsitem["subject"]);
	echo $template->displayTemplate("header.tpl");
	echo '<body>';

	?>
	<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="smalltext">
	<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><a href="index.php?_m=news&_a=viewnews&newsid=<?=$_newsitem["newsid"]?>" id="newstitle"><img src="<?=$_SWIFT["themepath"]?>icon_newsitembig.gif" border="0" align="absmiddle" />&nbsp;<?=$_newsitem["subject"]?></a></td></tr>
	<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><span class="newsdate"><?=$_SWIFT["language"]["nwpostedby"]?><?=$_SWIFT["staffcache"][$_newsitem["staffid"]]["fullname"]?> <?=$_SWIFT["languaged"]["nwpostedon"]?><?=edate($_SWIFT["settings"]["dt_datetimeformat"], $_newsitem["dateline"])?></span></td></tr>
	<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><span class="smalltext"><?=$_newsitem["description"]?></span></td></tr>
	<tr><td align="left" valign="top" colspan="2" style="PADDING: 3px;"><span class="mediumtext"><?=$_newsitem["contents"]?></span></td></tr>
	</table>
	<?
	echo $template->displayTemplate("footer.tpl");
}

?>